﻿#include "Curve2DKeySelcetBar.h"
#include "ui_Curve2DKeySelcetBar.h"
#include "DataChosenListDialog.h"
#include "IdaWidgetHelper.h"
#include <QModelIndexList>

namespace pst
{

    Curve2DKeySelcetBar::Curve2DKeySelcetBar(QWidget* parent)
        : QWidget(parent)
        , m_dataChosenListDialog(nullptr)
        //, m_text(nullptr)
        //, m_lineEdit(nullptr)
        //, m_button(nullptr)
        , m_selectedIndexs()
        , ui(new Ui::Curve2DKeySelcetBar())
    {
        ui->setupUi(this);
        this->setObjectName("Curve2DKeySelcetBar");
        this->setGeometry(0, 0, 100, 100);
        //auto layout = new QHBoxLayout(this);
        //m_text = new QLabel(this);
        //m_text->setTextPixelSize(14);
        //m_lineEdit = new QLineEdit(this);
        //m_lineEdit->setText(" ");
        //ui->lineEdit->setEnabled(false);
        //ui->lineEdit->setObjectName("Curve2DKeySelcetBarLineEdit");
        ui->label_ValueList->setStyleSheet("QLabel{border: 1px solid #DEDFE1;}");
        //m_button = new QPushButton("...");
        //layout->addWidget(m_text);
        //layout->addWidget(m_lineEdit);
        //layout->addStretch();
        //layout->addWidget(m_button);
        //ui->pushButton_SelcetBar->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_SelcetBar->setStyleSheet("QPushButton { text-align: center; }");
        ui->pushButton_SelcetBar->setIcon(QIcon(":/rc/apostrophe.png"));
        connect(ui->pushButton_SelcetBar, &QPushButton::clicked, this,
            &Curve2DKeySelcetBar::slot_selectDataButtonClicked);
    }

    Curve2DKeySelcetBar::~Curve2DKeySelcetBar()
    {
        delete ui;
    }

    void Curve2DKeySelcetBar::setData(
        const QString& keyName, const QList<double>& values)
    {
        m_values.clear();
        ui->label_text->setText(keyName);

        for (const auto& v : values)
        {
            m_values.append(QString::number(v));
        }
        if (!m_values.isEmpty())//默认选择第一个
        {
            m_selectedIndexs.clear();
            m_selectedIndexs << 0;
            ui->label_ValueList->setText(m_values[0]);
        }
    }

    void Curve2DKeySelcetBar::setData(
        const QString& keyName, const QList<QString>& values)
    {
        ui->label_text->setText(keyName);
        m_values = values;
        if (!m_values.isEmpty())//默认选择第一个
        {
            m_selectedIndexs.clear();
            m_selectedIndexs << 0;
            ui->label_ValueList->setText(m_values[0]);
        }
    }

    QList<int> Curve2DKeySelcetBar::getSelectedIndex()
    {
        return m_selectedIndexs;
    }

    void Curve2DKeySelcetBar::slot_sentChosenDataIds(
        const QModelIndexList& chosenID)
    {
        m_selectedIndexs.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + ",";

                auto index = chosenID.at(i).row();
                m_selectedIndexs.append(index);
            }
        }
        ui->label_ValueList->setText(strText);
        if (!m_selectedIndexs.isEmpty())
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBar, false, Qt::white);
    }
    void Curve2DKeySelcetBar::setMultipleSelected(bool isMultipleSelected)
    {
        m_isMultipleSelected = isMultipleSelected;
    }

    void Curve2DKeySelcetBar::setPushButtonSelcetBarSelectState(bool isSelect, const QColor& color)
    {
        IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_SelcetBar, isSelect, color);
    }
    QString Curve2DKeySelcetBar::getLabelText()
    {
        return ui->label_text->text();
    }

    void Curve2DKeySelcetBar::slot_selectDataButtonClicked()
    {
        //if (m_dataChosenListDialog == nullptr)
        //{
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        //m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setMultipleSelected(m_isMultipleSelected);
        m_dataChosenListDialog->setData(m_values);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs);//默认选中第一个

        connect(m_dataChosenListDialog,
            &DataChosenListDialog::signal_sentChosenDataIds,
            this, &Curve2DKeySelcetBar::slot_sentChosenDataIds);     
        //}
        //else
        //{
        //    m_dataChosenListDialog->showWindow();
        //}
    }

}
